const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
let name = ''
let information = ''
let sex = '男'
let image = ''
let OPENID
Page({
  data: {
    list:[],
    items:[
      {value:'男',name:'男'},
      {value:'女',name:'女'}
    ]
  },
  onLoad(options){
    OPENID = options.OpenID
    console.log('options为:',options)
    console.log('openid为:',OPENID)
    this.ConditionsQuery()
  },
  baocun(){
    if (image == '') {
        wx.showToast({
          icon: 'error',
          title: '头像不能为空',
      })
    } else if (name == '') {
      wx.showToast({
        icon: 'error',
        title: '昵称不能为空',
      })
    } else if (sex == '') {
      wx.showToast({
        icon: 'error',
        title: '性别不能为空',
      })
     } else if (information == '') {
      wx.showToast({
        icon: 'error',
        title: '简介不能为空',
      })
   } else {
    this.changeInformation()
    this.changeOrder()
    wx.navigateBack({
      url: '/pages/user/user'
    })
   }
  },
  // 性别选择
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    sex = e.detail.value
  },
  xuanzeIMG(e) {
    var that = this
    //让用户选择或拍摄一张照片
    wx.chooseImage({
      count: 1,	
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
      //选择完成会先返回一个临时地址保存备用
        that.uploadIMG(res.tempFilePaths[0],OPENID)
      }
    })
  },
  uploadIMG(temFile,openid){
    console.log('选择的图片临时路径为:',temFile)
    console.log('openid为:',openid)
            //将照片上传至云端需要刚才存储的临时地址
            let cloudPath = "touxiang/" + openid + ".jpg";
            wx.cloud.uploadFile({
              // cloudPath: "touxiang/"+ new Date().getTime() +"-"+ Math.floor(Math.random() * 1000),
              cloudPath,
              filePath:temFile,
              success:res=> {
              //上传成功后会返回永久地址
                 console.log(res.fileID) 
                 this.setData({
                  image:res.fileID,
                  imgURL:temFile
                 })
                 image = res.fileID
                 console.log('头像选择成功:',res)
              },
               fail:err=>{
                 console.log('头像选择成功:',err)
             }
            })
  },
  getname(e){
    name = e.detail.value
  },
  getsex(e){
    sex = e.detail.value
  },
  getinformation(e){
    information = e.detail.value
  },
  ConditionsQuery(){
    wx.cloud.database().collection('user-information')
    .where({
      _openid:OPENID
    })
    .get()
    .then(res=>{
      if(res.data[0]=='')
      console.log('用户信息未设置:')
      this.setData({
        list:res.data
      })
      console.log('用户信息查询成功:',res)
    })
    .catch(err=>{
      console.log('用户信息查询失败',err)
    })
  },
  changeInformation(){
    wx.cloud.database().collection('user-information')
    .where({
      _openid:OPENID
    })
    .update({
      data:{
        image:image,
        NAME:name,
        sex:sex,
        information:information
      }
    })
    .then(res=>{
      console.log('修改用户信息成功',res)
    })
    .catch(res=>{
      console.log('修改用户信息失败',res)
    })
  },
  changeOrder(){
    wx.cloud.database().collection('send-Goods')
    .where({
      _openid:OPENID
    })
    .update({
      data:{
        TOUXIANG:image,
        NAME:name,
      }
    })
    .then(res=>{
      console.log('用户订单个人信息同步成功',res)
    })
    .catch(res=>{
      console.log('用户订单个人信息同步失败',res)
    })
  }
})